package com.javasm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.AftersaleAppealCriteria;
import com.javasm.domin.entity.AftersaleAppeal;
import com.javasm.domin.vo.AftersaleAppealVo;
import com.javasm.mapper.AftersaleAppealMapper;
import com.javasm.service.AftersaleAppealService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.AftersaleAppealTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class AftersaleAppealServiceImpl extends BaseServiceimpl<AftersaleAppeal> implements AftersaleAppealService {

    private final AftersaleAppealMapper aftersaleAppealMapper;
    private final AftersaleAppealTransfer aftersaleAppealTransfer;

    @Override
    public PageResult<AftersaleAppealVo> searchPage(AftersaleAppealCriteria aftersaleAppealCriteria) {

        //开启分页
        PageHelper.startPage(aftersaleAppealCriteria.getCurrentPage(),aftersaleAppealCriteria.getPageSize());
        //条件创建
        LambdaQueryWrapper<AftersaleAppeal> lambda = new QueryWrapper<AftersaleAppeal>().lambda();
        if(aftersaleAppealCriteria.getEstablishId()!=null&&aftersaleAppealCriteria.getEstablishId()!=""){
            lambda.like(AftersaleAppeal::getEstablishId, aftersaleAppealCriteria.getEstablishId());
        }

        if(!StringUtils.isEmpty(aftersaleAppealCriteria.getEstablishstartTime())){
            lambda.between(AftersaleAppeal::getEstablishTime, aftersaleAppealCriteria.getEstablishstartTime(), aftersaleAppealCriteria.getEstablishendTime());
        }

        if(aftersaleAppealCriteria.getRecordState()!=null&&aftersaleAppealCriteria.getRecordState()!=0){
            lambda.eq(AftersaleAppeal::getRecordState, aftersaleAppealCriteria.getRecordState());
        }
        if(aftersaleAppealCriteria.getAppealType()!=null&&aftersaleAppealCriteria.getAppealType()!=0){
            lambda.eq(AftersaleAppeal::getAppealType, aftersaleAppealCriteria.getAppealType());
        }
        if(aftersaleAppealCriteria.getApproverId()!=null&&aftersaleAppealCriteria.getApproverId()!=""){
            lambda.like(AftersaleAppeal::getApproverId, aftersaleAppealCriteria.getApproverId());
        }

        if(!StringUtils.isEmpty(aftersaleAppealCriteria.getStartTime())){
            lambda.between(AftersaleAppeal::getApproverTime, aftersaleAppealCriteria.getStartTime(), aftersaleAppealCriteria.getEndTime());
        }

        List<AftersaleAppeal> aftersaleB2cChanges = aftersaleAppealMapper.selectList(lambda);
        /*获取总条数*/
        PageInfo<AftersaleAppeal> aftersaleOutPageInfo = new PageInfo<>(aftersaleB2cChanges);
        return new PageResult<AftersaleAppealVo>(aftersaleOutPageInfo.getTotal(),aftersaleAppealTransfer.jotoVo(aftersaleB2cChanges));

    }
}
